#region License
/* Copyright (c) 2011 Maksymilian Mulawa
	
Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice, 
 * this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution.
   3. The name of the author may not be used to endorse or promote products 
 * derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

namespace ReplicationExplorer.DomainModel.UI
{
    public class StringConverter
    {
        public static string ConvertBoolToYesNo(bool value)
        {
            return value ? "True" : "False";
        }

        public static string ConvertToHoursOrDays(int hours)
        {
            string result;
            if(hours % 24 == 0)
            {
                result = string.Format("{0} days",hours / 24);
            }
            else
            {
                result = string.Format("{0} hours", hours);
            }

            return result;
        }

        public static string FormatToSeconds(int seconds)
        {
            return string.Format("{0} seconds", seconds);
        }

        public static string ConvertMBToGB (int megabytes)
        {
            double gigabytes = megabytes/1024.0;
            return string.Format("{0:0.000} GB", gigabytes);
        }

        /// <summary>
        /// 1 = Started
        /// 2 = Succeeded
        /// 3 = In progress
        /// 4 = Idle
        /// 5 = Retrying
        /// 6 = Failed
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public static string ConvertPublisherStatus (int status)
        {
            string result;
            switch (status)
            {
                case  1:
                    result = "Started";
                    break;
                case 2:
                    result = "Succeeded";
                    break;

                case 3:
                    result = "In progress";
                    break;

                case 4:
                    result = "Idle";
                    break;

                case 5:
                    result = "Retrying";
                    break;

                case 6:
                    result = "Failed";
                    break;
                default:
                    result = "N/A";
                    break;

            }

            return result;
        }

        public static string ConvertPublisherWarning(int warning)
        {
            //Warning message source: http://msdn.microsoft.com/en-us/library/ms174423.aspx
            string warningMessage = string.Empty;
            int warningCount = 0;

            if ((warning & 1) != 0)
            {
                warningCount++;
                warningMessage += warningCount + ") a subscription to a transactional publication has not been synchronized within the retention period threshold.\n";
            }

            if ((warning & 2) != 0)
            {
                warningCount++;
                warningMessage += "the time taken to replicate data from a transactional Publisher to the Subscriber exceeds the threshold, in seconds\n";
            }

            if ((warning & 4) != 0)
            {
                warningCount++;
                warningMessage += warningCount + ") a subscription to a merge publication has not been synchronized within the retention period threshold. \n";
            }

            if ((warning & 8) != 0)
            {
                warningCount++;
                warningMessage += warningCount + ") the time taken to complete synchronization of a merge subscription exceeds the threshold, in seconds, over a fast network connection.\n";
            }

            if ((warning & 16) != 0)
            {
                warningCount++;
                warningMessage += warningCount + ") the time taken to complete synchronization of a merge subscription exceeds the threshold, in seconds, over a slow or dial-up network connection\n";
            }

            if ((warning & 32) != 0)
            {
                warningCount++;
                warningMessage += warningCount + ") the delivery rate for rows during synchronization of a merge subscription has failed to maintain the threshold rate, in rows per second, over a fast network connection.\n";
            }

            if ((warning & 64) != 0)
            {
                warningCount++;
                warningMessage += warningCount + ") the delivery rate for rows during synchronization of a merge subscription has failed to maintain the threshold rate, in rows per second, over a slow or dial-up network connection.\n";
            }

            if (string.IsNullOrEmpty(warningMessage))
            {
                warningMessage = "None";
            }
            return warningMessage;
        }

    }
}